#!/usr/bin/env python
#
# Copyright (c) 2010 David Hamrick, Guy Kopsombut, Emily Ross 
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

from google.appengine.ext import db
from django.utils import simplejson as json

import logging

class Cab(db.Model):
	name = db.StringProperty(required=False)
	longitude = db.StringProperty(required=False)
	latitude = db.StringProperty(required=False)
	def toDict(self):
		cabDict = dict()
		cabDict['id'] = self.key().id()
		cabDict['name'] = self.name
		cabDict['latitude'] = self.latitude
		cabDict['longitude'] = self.longitude
		return cabDict	
	def printJson(self):
		return json.dumps(self.toDict())
	def importJson(self,jsonStr):
		jsonObj = json.loads(jsonStr)
		logging.info("BLAH BLAH")
		self.name = jsonObj['name']
		self.latitude = jsonObj['latitude']
		self.longitude = jsonObj['longitude']
		self.put()

class Passenger(db.Model):
	name = db.StringProperty(required=False)
	address = db.StringProperty(required=False)
	phone = db.StringProperty(required=False)
	cabid = db.StringProperty(required=False)
	status = db.StringProperty(required=False)
	def toDict(self):
		passDict = dict()
		passDict['id'] = self.key().id()
		passDict['name'] = self.name
		passDict['address'] = self.address
		passDict['phone'] = self.phone
		passDict['cabid'] = self.cabid
		passDict['status'] = self.status
		return passDict
	def printJson(self):
		return json.dumps(self.toDict())
	def importJson(self,jsonStr):
		jsonObj = json.loads(jsonStr)
		self.name = jsonObj['name']
		self.address = jsonObj['address']
		self.phone = jsonObj['phone']
		if 'cabid' in jsonObj:
			self.cabid = jsonObj['cabid']
		else:
			self.cabid = 'null';
		if 'status' in jsonObj:
			self.status = jsonObj['status']
		else:
			self.status = 'null'
		self.put()

class CabPassLink(db.Model):
	name = db.StringProperty(required=False)
	address = db.StringProperty(required=False)
	phone = db.StringProperty(required=False)
	def __str__(self):
		return json.dumps(self.toDict())
	def toDict(self):
		passDict = dict()
		passDict['id'] = self.key().id()
		passDict['cabid'] = self.cabid
		passDict['passid'] = self.passid
		passDict['timereq'] = self.timereq
		passDict['status'] = self.status
		passDict['timecomp'] = self.timecomp
		return passDict
	def importJson(self,jsonStr):
		jsonObj = json.loads(jsonStr)
		self.cabid = jsonObj['cabid']
		self.passid = jsonObj['passid']
		self.timereq = jsonObj['timereq']
		self.status = jsonObj['status']
		self.timecomp = jsonObj['timecomp']
		self.put()
